<?php

// $Id$

global $WSC;

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


abstract class TCMS_CIA extends Tangra_Class implements I_DB_Storable {
	private $id;
	private $ci;
	private $ci_type_attr;
	private $value;

	public function set_id($id) {
		tangra_if_not_int_throw_e($id);

		$this->id = $id;
	}


	public function get_id() {
		return $this->id;
	}


	public function set_ci($ci) {
		tangra_if_not_int_throw_e($ci);

		$this->ci = $ci;
	}


	public function get_ci() {
		return $this->ci;
	}


	public function set_ci_type_attr($ci_type_attr) {
		tangra_if_not_int_throw_e($ci_type_attr);

		$this->ci_type_attr = $ci_type_attr;
	}


	public function get_ci_type_attr() {
		return $this->ci_type_attr;
	}


	public function set_value($value) {
		if ($this->is_valid_value($value)) {
			$this->_set_value($value);
		} else {
			throw new Tangra_Module_Exception('Invalid date for '.get_class($this).'. Value: '.$value);
		}
	}


	private function _set_value($value) {
		$this->value = $value;
	}


	public function get_value() {
		return $this->value;
	}


	abstract public function is_valid_value($value);


	public function get_valid_values() {
		return array();
	}


	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);

		}

		return $ret;
	}


	abstract public function delete(DB_Connection $dbc, $id);

	abstract public function get_sql_select_id_ci_for_ci_type_attr($ci_type_attr);

	abstract public function count_all_for_ci_type_attr(DB_Connection $dbc, $ci_type_attr);

	abstract public function delete_all_for_ci(DB_Connection $dbc, $ci_id);
}